precision mediump float;

uniform sampler2D u_texture;
uniform vec4 u_ambientLight0;//mau xung quanh
uniform vec4 u_light_diffuse0;// mau khuyech tan
uniform vec4 u_specularl0;// mau phan chieu 							
uniform vec3 u_light_pos0;

uniform vec4 u_ambientLight1;//mau xung quanh
uniform vec4 u_light_diffuse1;// mau khuyech tan
uniform vec4 u_specularl1;// mau phan chieu 							
uniform vec3 u_light_pos1;


uniform vec3 u_camera_pos;
varying vec2 v_uv;
varying vec3 v_normal;
varying vec3 v_pos;

// bieu thuc trn tinh cho 1 nguon sang
//khi co nhieu nguon sang ta se cong tong DiffuseComponent va  SpecularComponent roi the vao cong thuc cuoi
void main()
{
	vec4 ObjColor = texture2D(u_texture, v_uv );
		vec3 v_toEye = normalize(u_camera_pos - v_pos).xyz;
	
	


//light 0	
    vec3 v_lightDirection = normalize(v_pos- u_light_pos0).xyz;
    vec4 AmbientComponent;
    vec4 DiffuseComponent;
    vec4 reflectVector;
    vec4 SpecularComponent;
	
	
	AmbientComponent = u_ambientLight0;//vec4(1.0,1.0,1.0,1.0);
	DiffuseComponent = max(0.0,dot(v_normal,-v_lightDirection)) * u_light_diffuse0;
	
	reflectVector = normalize(reflect(vec4(v_lightDirection, 0.0), vec4(v_normal, 0.0)));
	SpecularComponent = pow(max(dot(reflectVector, vec4(v_toEye, 0.0)), 0.0), 20.0) * u_specularl0;

//light 1	
	vec3 v_lightDirection1 = normalize(v_pos- u_light_pos1).xyz;
    vec4 AmbientComponent1;
	vec4 DiffuseComponent1;
	vec4 reflectVector1;
	vec4 SpecularComponent1;
	
	
	AmbientComponent1 = u_ambientLight1;//vec4(1.0,1.0,1.0,1.0);
	DiffuseComponent1 = max(0.0,dot(v_normal,-v_lightDirection1)) * u_light_diffuse1;
	
	reflectVector1 = normalize(reflect(vec4(v_lightDirection1, 0.0), vec4(v_normal, 0.0)));
	SpecularComponent1 = pow(max(dot(reflectVector1, vec4(v_toEye, 0.0)), 0.0), 20.0) * u_specularl1;
//------------------------------all-------------------------------
DiffuseComponent = DiffuseComponent + DiffuseComponent1;
SpecularComponent = SpecularComponent + SpecularComponent1;

//gl_FragColor = (AmbientComponent*0.3) + DiffuseComponent*(1.0-0.3)) * ObjColor + SpecularComponent;
//gl_FragColor = AmbientComponent*0.3 + (DiffuseComponent*0.7) * ObjColor + SpecularComponent;
	gl_FragColor = (AmbientComponent + DiffuseComponent) * ObjColor + SpecularComponent;
	
}
